<div class="wrap">
    <h2>Add Video</h2>
    <form id="html5av-video-add-panel">
        <p>
            <label for="html5av-video-add-panel-title">Title:</label><br />
            <input type="text" id="html5av-video-add-panel-title"
                   style="width: 99%"/>
        </p> 

        <p>
            <label for="html5av-video-add-panel-alt">Alt:</label><br />
            <input type="text" id="html5av-video-add-panel-alt" 
                   style="width: 99%"/>
        </p>

        <p style="float:left; margin-right: 5px;">
            <label for="html5av-video-add-panel-width">Width:</label><br />
            <input type="text" id="html5av-video-add-panel-width" />
        </p>

        <p style="float:left; margin-right: 5px;">
            <label for="html5av-video-add-panel-height">Height:</label><br />
            <input type="text" id="html5av-video-add-panel-height" />
        </p>

        <div style="clear: both;"></div>

        <p>
            <label for="html5av-video-add-panel-view">View:</label><br />
            <select id="html5av-video-add-panel-view" >
                <?php foreach ($videoViews as $value => $selected) : ?>
                    <option value="<?php echo $value ?>"
                            <?php echo $selected ?> ><?php echo $value ?></option>
                        <?php endforeach; ?>
            </select>
        </p>

        <h3><label for="html5av-video-add-panel-file">Poster: (Upload Only and Mandatory)</label></h3>
        <input type="file" id="html5av-video-add-panel-file" />

        <p>Note: add video file while in editor!</p>

        <input type="submit"
               value="Save and Open Editor" />
        
        <p>Note: This button only works while posted is selected!</p>
    </form>
</div>

<div id="html5av-post-video-upload-editor"></div>

<script type="text/javascript">
    var html5av_api = '<?php echo html5av_manager::getApiURL(); ?>';
    var html5av_uploadifyLoc = '<?php echo html5av_manager::getUrl(); ?>/lib/uploadify';
    var html5av_add_object_post;
    
    function html5avAddVideoCheck() {
        var title = jQuery('#html5av-video-add-panel-title').val();
        var width = jQuery('#html5av-video-add-panel-width').val();
        var height = jQuery('#html5av-video-add-panel-height').val();
        var noError = true;
        var msg = '';
        if(title == '') {
            noError = false;
            msg += "Title cannot be left blank\n";
        }
        
        if(width == '') {
            noError = false;
            msg += "Width cannot be left blank\n";
        } else if(isNaN(width)) {
            noError = false;
            msg += "Width is not a number\n";
        }
        
        if(height == '') {
            noError = false;
            msg += "Height cannot be left blank\n";
        } else if(isNaN(height)) {
            noError = false;
            msg += "Height is not a number\n";
        }
        
        if(!noError) {
            alert(msg);
        }
        
        return noError;
    }
    
    jQuery(function($) {
        $('#html5av-post-video-upload-editor').dialog({
            autoOpen: false,
            width: 800,
            height: 600,
            minWidth: 800,
            minHeight: 600,
            modal: true
        });
        
        $('#html5av-video-add-panel-file').html5avUploadify({
            'uploader'  : html5av_uploadifyLoc+'/uploadify.swf',
            'script'    : html5av_uploadifyLoc+'/custom.php',
            'cancelImg' : html5av_uploadifyLoc+'/cancel.png',
            'folder'    : '/temp',
            'removeCompleted' : true,
            'multi'     : false,
            'onComplete': function(event, ID, fileObj, response, data) {
                var json = $.parseJSON(response);
                var post = html5av_add_object_post;
                post.file = json.file;
                post.path = json.path;
                post.node = 'add_new_video';
                $.post(ajaxurl, post, function(id) {
                    var post = {
                        action: 'html5av_manager',
                        node:'get_media_editor',
                        avid:id
                    }
                    jQuery.post(ajaxurl, post, function(data) {
                        jQuery('#html5av-video-add-panel-title').val('');
                        jQuery('#html5av-video-add-panel-alt').val('');
                        jQuery('#html5av-video-add-panel-width').val('');
                        jQuery('#html5av-video-add-panel-height').val('');
                        if(jQuery('#html5av-widget').size() > 0) {
                            jQuery('#html5avmanagerid').val(id);
                            html5avWidgetCheck();
                            jQuery('#html5av-widget-editor-dialog').html(data);
                            jQuery('#html5av-widget-editor-dialog').dialog('open');
                            jQuery('#html5av-widget-add-video-dialog').dialog('close');
                            jQuery('#html5av-widget-add-video-dialog').html('');
                        } else {
                            jQuery('#html5av-post-video-upload-editor').html(data);
                            jQuery('#html5av-post-video-upload-editor').dialog('open');
                        }
                    });
                });
            },
            'fileExt':'*.jpg;*.jpeg',
            'fileDesc':'Jpeg Files (jpg,jpeg)',
            'buttonText':'Select Jpeg File'
        });
        
        $('#html5av-video-add-panel').submit(function() {
            if(html5avAddVideoCheck()) {
                var title = jQuery('#html5av-video-add-panel-title').val();
                var alt = jQuery('#html5av-video-add-panel-alt').val();
                var width = jQuery('#html5av-video-add-panel-width').val();
                var height = jQuery('#html5av-video-add-panel-height').val();
                var view = jQuery('#html5av-video-add-panel-view').val();
                html5av_add_object_post = {
                    action: 'html5av_manager',
                    title:title,
                    alt:alt,
                    width:width,
                    height:height,
                    view:view
                };
                $('#html5av-video-add-panel-file').html5avUploadifyUpload();
            }
            return false;
        });
    });
</script>